using System;
using System.IO;
using UnityEngine;
using LS.Digitize;

namespace LS.Digitize
{
    public sealed class RectSerializer: CompoundSerializer<Rect>
    {
        public RectSerializer() : base(typeof(Rect),typeof(float))
        {
            
        }
        
        public override void WriteData(IWriter writer, Rect obj)
        {
            ISerializer subSerializer = SubSerializer;
            using (writer.WriteObjectScope())
            {
                if (subSerializer is PrimitiveSerializer<float> ps)
                {
                    using (writer.WritePropertyScope("x"))
                        ps.WriteData(writer, obj.x);
                    
                    using (writer.WritePropertyScope("y"))
                        ps.WriteData(writer, obj.y);
                    
                    using (writer.WritePropertyScope("width"))
                        ps.WriteData(writer, obj.width);
                    
                    using (writer.WritePropertyScope("height"))
                        ps.WriteData(writer, obj.height);
                }
                else
                {
                    using (writer.WritePropertyScope("x"))
                        subSerializer.Write(writer, obj.x);
                    
                    using (writer.WritePropertyScope("y"))
                        subSerializer.Write(writer, obj.y);
                    
                    using (writer.WritePropertyScope("width"))
                        subSerializer.Write(writer, obj.width);
                    
                    using (writer.WritePropertyScope("height"))
                        subSerializer.Write(writer, obj.height);
                }
            }
        }

        public override Rect ReadData(IReader reader)
        {
            ISerializer subSerializer = SubSerializer;
            Rect target = default;
            using (var objScope = reader.ReadObjectScope())
            {
                string propertyName = null;
                if (subSerializer is PrimitiveSerializer<float> ps)
                {
                    propertyName = reader.ReadPropertyName();
                    float x = ps.ReadData(reader);
                    propertyName = reader.ReadPropertyName();
                    float y = ps.ReadData(reader);
                    propertyName = reader.ReadPropertyName();
                    float width = ps.ReadData(reader);
                    propertyName = reader.ReadPropertyName();
                    float height = ps.ReadData(reader);
                    target = new Rect(x, y, width, height);
                }
                else
                {
                    propertyName = reader.ReadPropertyName();
                    float x = (float)subSerializer.Read(reader);
                    propertyName = reader.ReadPropertyName();
                    float y = (float)subSerializer.Read(reader);
                    propertyName = reader.ReadPropertyName();
                    float width = (float)subSerializer.Read(reader);
                    propertyName = reader.ReadPropertyName();
                    float height = (float)subSerializer.Read(reader);
                    target = new Rect(x, y, width, height);
                }
            }

            return target;
        }
        
        public override void ReadInto(IReader reader, ref Rect obj)
        {
            obj = ReadData(reader);
        }
    }
}